package cn.ekuma.pos.pda.sale;

import java.util.HashMap;

import android.app.Activity;
import android.app.AlertDialog;
import android.content.Intent;
import android.os.Bundle;
import android.os.Handler;
import android.os.Message;
import android.widget.Toast;
import cn.ekuma.android.keyBoardinput.KeyBoradInputPanel;
import cn.ekuma.android.ui.scan.CaptureScanActivity;
import cn.ekuma.data.dao.bean.I_ViewBean;
import cn.ekuma.data.ui.android.AbstractFinderActivate;
import cn.ekuma.data.ui.android.OnBaseBeanSelectedListener;
import cn.ekuma.epos.android.finder.AdvProductSelectActivate;
import cn.ekuma.epos.android.finder.CustomerFinderActivate;
import cn.ekuma.pos.pda.sale.TicketLineEditor.OnSelectedActionListener;

import com.android.calculator2.Logic;
import com.android.calculator2.Logic.OnReturnListener;
import com.openbravo.bean.Customer;
import com.openbravo.bean.sales.SharedTicketInfo;
import com.openbravo.bean.sales.Ticket;
import com.openbravo.bean.sales.TicketLine;

public abstract class AbstractTicktSaleActivity extends Activity implements OnBaseBeanSelectedListener,OnReturnListener{

	
	public static final int SELECT_TICKET=0;
	public static final int SELECT_PRODUCTS=1;
	public static final int SELECT_TABLE=2;
	public static final int SELECT_CUSTOMER=3;
	public static final int SCAN_PRODUCTS=4;
	public static final int RETURN_TYPE_TOTAL=SCAN_PRODUCTS;
	
	protected TicketLines ticketList;
	protected TicketLineListAdapter lineAdapter;
	protected KeyBoradInputPanel keyInportPanel;

	private String shardTicketId;
	
	protected TicketManager ticketManager;
	
	
	public void onCreate(Bundle savedInstanceState) {
		super.onCreate(savedInstanceState);
		ticketManager=new TicketManager(this,ticketHandler);
	}
	
	protected void init() {
	    ticketManager.init();
	    ticketManager.newTicket();
     }
	
	@Override
	protected void onActivityResult(int requestCode, int resultCode, Intent data) {
		if(data==null)
			return;
		HashMap<String,Double> selectMap;
		switch(requestCode){
		case SELECT_TICKET:
			SharedTicketInfo selectTicketInfo=(SharedTicketInfo) data.getSerializableExtra(AbstractFinderActivate.SELECTED_BEAN);
			ticketManager.loadShardTicket(selectTicketInfo.getId());
			break;
		case SELECT_PRODUCTS:
			selectMap=(HashMap<String,Double>) data.getSerializableExtra(AdvProductSelectActivate.SELECTED_BEANS);
			ticketManager.addSelectedProduct(selectMap);
			break;
		case SCAN_PRODUCTS:
			selectMap=(HashMap<String,Double>) data.getSerializableExtra(CaptureScanActivity.SELECTED_BEANS);
			ticketManager.addSelectedProduct(selectMap);
			break;
		case SELECT_CUSTOMER:
			final Customer cu=(Customer)data.getSerializableExtra(CustomerFinderActivate.SELECTED_BEAN);
			ticketManager.changeCustomer(cu);
			break;
		case SELECT_TABLE:
			break;
		}
	}
	
	protected void refushTicket(){
    	lineAdapter.clear();
    	Ticket currentTicket=ticketManager.getCurrentTicket();
    	for(TicketLine info:currentTicket.getLines())
    		lineAdapter.add(info);
    	ticketList.getListview().setSelection(currentTicket.getLines().size());
		refushState();
    }
	
	protected abstract void refushState();
	
	protected Handler ticketHandler = new Handler() {  
        public void handleMessage (Message msg) {//此方法在ui线程运行   
            switch(msg.what) {  
            case TicketManager.MSG_SUCCESS:  
            	refushTicket();
                break;  
            case TicketManager.MSG_FAILURE:  
                Toast.makeText(getApplication(), "执行出错了", Toast.LENGTH_LONG).show();  
                break;
            case TicketManager.MSG_NO_FIND_PRODUCT:
            	Toast.makeText(getApplication(), "没找到指定的商品", Toast.LENGTH_LONG).show();  
                break;
            case TicketManager.MSG_REFULSH_TICKET:
            	refushState();
                break;
            case TicketManager.MSG_TICKETLINE_ADD:
            	Ticket currentTicket=ticketManager.getCurrentTicket();
            	lineAdapter.add(currentTicket.getLine(currentTicket.getLines().size()-1));
            	ticketList.setSelectionAtPosition(currentTicket.getLines().size()-1);
                break; 
            case TicketManager.MSG_TICKETLINE_REMOVE:
            	int position=(Integer) msg.obj;
            	lineAdapter.remove(position);
            	ticketList.setSelectionAtPosition(position);
                break;       
            }  
        }
    };
	
	
	public void baseBeanSelected(I_ViewBean obj) {
    	AlertDialog.Builder builder = new AlertDialog.Builder(this);
    	TicketLineEditor inputView=new TicketLineEditor(this);
        builder.setView(inputView);
        final AlertDialog dialog=builder.create();
        inputView.setOnSelectedActionListener(new OnSelectedActionListener(){
			public void onOkAction(TicketLine editLine) {
				dialog.dismiss();
				lineAdapter.notifyDataSetInvalidated();
				refushState();
			}
        }); 
        inputView.resert((TicketLine) obj);
        dialog.show();  
		
	}

	public void onReturn(String value) {
		if(value==null||value.length()==0)
			return;
		HashMap<String,Double> maps=new HashMap<String,Double>();
		maps.put(value, 1.0);
		ticketManager.addSelectedProduct(maps);
	}

	public void onCommand(int command,String value) {
		double val=0;
		if(value!=null&&value.length()>0){
			val=Double.valueOf(value);
		     if(command==Logic.COMMAND_plus){
		    	 ticketManager.addSelectedProduct(val);
		     }
		}else{
			
		}
	}
	
	 protected void startActivate(Class other){
	    	Intent intent = new Intent();
	    	intent.setClass(this, other);
	    	startActivity(intent);
	    }
}
